package com.sharp.core.voxel;


import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;

public class GridCell {
	private float[] values;
	private Vector3f[] vertices;
	private ColorRGBA color;
	
	public GridCell(Vector3f pos, float size) {
		vertices = new Vector3f[8];
		/*vertices[0] = new Vector3f(pos);
		vertices[1] = pos.add(size, 0, 0);
		vertices[2] = pos.add(size, 0, size);
		vertices[3] = pos.add(0, 0, size);
		vertices[4] = pos.add(0, size, 0);
		vertices[5] = pos.add(size, size, 0);
		vertices[6] = pos.add(size, size, size);
		vertices[7] = pos.add(0, size, size);*/
		
		vertices[0] = new Vector3f(pos);
		vertices[1] = pos.add(size, 0, 0);
		vertices[2] = pos.add(size, size, 0);
		vertices[3] = pos.add(0, size, 0);
		vertices[4] = pos.add(0, 0, size);
		vertices[5] = pos.add(size, 0, size);
		vertices[6] = pos.add(size, size, size);
		vertices[7] = pos.add(0, size, size);
		
		values = new float[8];
	}

	public boolean isInLevel(float isoLevel) {
		float value = 0;
		for ( float v : values ) {
			value += v >= isoLevel ? 1 : -1;
		}
		return value > -8 && value < 8;
	}
	
	public int getCubeIndex(float isoLevel) {
		int cubeIndex = 0; 
		if (values[0] < isoLevel) cubeIndex |= 1;
		if (values[1] < isoLevel) cubeIndex |= 2;
		if (values[2] < isoLevel) cubeIndex |= 4;
		if (values[3] < isoLevel) cubeIndex |= 8;
		if (values[4] < isoLevel) cubeIndex |= 16;
		if (values[5] < isoLevel) cubeIndex |= 32;
		if (values[6] < isoLevel) cubeIndex |= 64;
		if (values[7] < isoLevel) cubeIndex |= 128;
		return cubeIndex;
	}

	public Vector3f getVertex(int i) {
		return vertices[i];
	}
	
	public float getValue(int i) {
		return values[i];
	}
	
	public void setValue(int i, float v) {
		values[i] = v;
	}
	
	public void setVertex(int i, Vector3f v) {
		vertices[i] = v;
	}

	public void setColor(ColorRGBA color) {
		this.color = color;
	}
	
	public ColorRGBA getColor() {
		return color;
	}
}
